<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 黄老邪 <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_guestbook\controller;

use cmf\controller\PluginAdminBaseController;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{

    public function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        } else {
            $this->error('未登录');
        }
    }

    /**
     * 留言管理
     * @adminMenu(
     *     'name'   => '留言管理',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '千佰特留言板管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $where['content'] = ['like','%'.$param['keyword'].'%'];
        } else{
            $where = '';
        }
        $lists = Db::name('guestbook')->where($where)->order('id desc')->paginate(10);
        $page = $lists->render();

        $this->assign('page', $page);
        $this->assign('lists', $lists);
        return $this->fetch('/admin_index');
    }


    public function delete(){
        Db::name('guestbook')->where(['id'=>$this->request->param('id', 0, 'intval')])->delete();
        $this->success('删除成功');
    }

    public function deletes(){
        $param = $this->request->param();
        foreach($param['ids'] as $k=>$v){
            Db::name('guestbook')->where('id',$v)->delete();
        }
        //Db::name('guestbook')->where(['id'=>$this->request->param('id', 0, 'intval')])->delete();
        $this->success('删除成功');
    }

}
